
#include "Main.h"
#include "FileTools.h"
#include "Controller/Controller_Main.h"
#include "Preset/Preset.h"

// Externals
#include <archive.h>
#include <archive_entry.h>
#include <fcntl.h>
//#include <io.h> -- Not sure what this is for, but OSX doesn't have it...

//-------------------------------------------------------------------
//                          FFileInfo
//-------------------------------------------------------------------

FFileInfo::FFileInfo()
{
	m_szFullPathAndName[0] = 0;
	m_szName[0] = 0;
	m_bIsDirectory = false;	
}

FFileInfo::FFileInfo( const char* szFullPathAndName, const bool bIsDirectory )
{
	m_szFullPathAndName[0] = 0;
	m_szName[0] = 0;
	
	m_bIsDirectory = bIsDirectory;
	
	// Copy the full path and name
	const u_int uLen = strlen( szFullPathAndName ) + 1;
	
	strncpy( &m_szFullPathAndName[0], szFullPathAndName, MACRO_MIN( uLen, uFFileInfo_MAX_PATH ) );
	m_szFullPathAndName[ uLen - 1 ] = 0;

	// Replace all the backslashes with foreslashes if necessary
	char* pcChar = m_szFullPathAndName;
	while( *pcChar )
	{
		if( *pcChar == '\\' )
		{
			*pcChar = '/';
		}
		++pcChar;
	}

	// Make the 'name'
	pcChar = 0;
	if( !m_bIsDirectory )
	{
		pcChar = strstr( m_szFullPathAndName, "." );
		--pcChar;
	}
	
	if( !pcChar )
	{
		pcChar = &m_szFullPathAndName[ uLen - 3 ];
	}
	int iCount = 0;
	
	while( ( *pcChar != '/' ) && ( pcChar != m_szFullPathAndName ) )
	{
		--pcChar;
		++iCount;
	}

	++pcChar;
	++iCount;

	strncpy( &m_szName[0], pcChar, MACRO_MIN( (u_int)iCount, uFFileInfo_MAX_NAME ) );
	
	m_szName[ iCount - 1 ] = 0;
}

FFileInfo::~FFileInfo()
{

}

//-------------------------------------------------------------------
//                          FileTools
//-------------------------------------------------------------------

static const u_int uFILETOOL_STRING_VERSION = 0;

const char* FileTools::szFRIDGE_INTERNAL_PRESET_FILE = "/Preset.dat";
const char* FileTools::szFRIDGE_INTERNAL_RESOURCE_LIST_FILE = "/ResourceList.dat";

void FileTools::FindFiles ( SuperTree<FFileInfo>* pxFileList, FRIDGE_FILE_TYPES eType, const bool bRecursive )
{
	Platform_FindFiles( pxFileList, eType, bRecursive );
}

bool FileTools::SavePrompt( FRIDGE_FILE_TYPES eType, char* szSaveFile )
{
	bool returnValue = Platform_SavePrompt( eType, szSaveFile );
	FridgeLogf("SAVE FILE: %s\n", szSaveFile);
	return returnValue;
}

bool FileTools::OpenPrompt( FRIDGE_FILE_TYPES eType, char* szOpenFile )
{
	bool returnValue = Platform_OpenPrompt( eType, szOpenFile );
	FridgeLogf("OPEN FILE: %s\n", szOpenFile);
	return returnValue;	
}

void FileTools::WriteStringToFile( const char* szString, FILE* pxFile )
{
    if( !pxFile )
    {
            return;
    }

    fwrite( &uFILETOOL_STRING_VERSION, sizeof( u_int ), 1, pxFile );

    int iStrlen = strlen( szString );

    fwrite( &iStrlen, sizeof( int ), 1, pxFile );
    if( !iStrlen )
    {
            return;
    }
    //TODO: might need to break up strings in 1k chunks
    fwrite( szString, iStrlen, 1, pxFile );
}

void FileTools::ReadStringFromFile( char*& szString, FILE* pxFile, bool bAllocateString )
{
    if( !pxFile )
    {
            return;
    }

    u_int uVersion = 0;
    fread( &uVersion, sizeof( u_int ), 1, pxFile );

    int iStrlen;

    fread( &iStrlen, sizeof( int ), 1, pxFile );
    if( !iStrlen )
    {
            return;
    }
    if( bAllocateString )
    {
            szString = new char[ iStrlen + 1 ];
    }

    //TODO: might need to break up strings in 1k chunks
    fread( szString, iStrlen, 1, pxFile );
    szString[ iStrlen ] = 0;
}

void FileTools::MakeTemporaryFile( FRIDGE_INTERNAL_FILES eType )
{
    char szFilename[ FRIDGE_MAX_PATH ];

    // TODO: Switch might be better here, but right now we only have two internal files to make, so it doesn't really matter...
    if ( eType == FRIDGE_INTERNAL_PRESET )
    {
        GetTempDirectory( FRIDGE_DIR_TEMPORARY_PRESET, szFilename);
        strncat( szFilename, szFRIDGE_INTERNAL_PRESET_FILE, FRIDGE_MAX_PATH );
    }
    else
    {
        GetTempDirectory( FRIDGE_DIR_TEMPORARY_RESOURCES, szFilename );
        strncat(szFilename, szFRIDGE_INTERNAL_RESOURCE_LIST_FILE, FRIDGE_MAX_PATH );
    }

    FILE* pxFile = 0;
    pxFile = fopen( szFilename, "wb" );
    FAssert( pxFile != 0 );
    fclose( pxFile );
}

bool FileTools::MakeDirectory( FRIDGE_TEMP_DIRECTORIES eType )
{
    char szPath[ FRIDGE_MAX_PATH ];
    GetTempDirectory( eType, szPath );
    return Platform_MakeDirectory( szPath );
}

bool FileTools::RenameDirectory( const char* szOldDirectory, const char* szNewDirectory )
{
	return Platform_RenameDirectory( szOldDirectory, szNewDirectory );
}

bool FileTools::RemoveTempDirectory()
{
	return Platform_RemoveTempDirectory();
}

void FileTools::GetTempDirectory( FRIDGE_TEMP_DIRECTORIES eType, char* szPath )
{
    const char* szPresetName = Controller_Main::GetCurrentPreset()->GetFilename();

    // Add preset file name to directory if necessary
    if( eType == FRIDGE_DIR_TEMPORARY_PRESET )
    {
        sprintf( szPath, "%s/%s", Platform_GetTempDirectoryForType( FRIDGE_DIR_TEMPORARY_APP ), szPresetName );
    }
    else if( eType == FRIDGE_DIR_TEMPORARY_RESOURCES )
    {
        sprintf( szPath, "%s/%s/Resources", Platform_GetTempDirectoryForType( FRIDGE_DIR_TEMPORARY_APP ), szPresetName );
    }
    else
    {
        strncpy( szPath, Platform_GetTempDirectoryForType( eType ), FRIDGE_MAX_PATH );
    }
}

//eof
